package cn.wolfcode.web.controller;

import cn.wolfcode.web.domain.Permission;
import cn.wolfcode.web.jsonResult.JsonResult;
import cn.wolfcode.web.query.QueryObject;
import cn.wolfcode.web.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PremissionController {
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo) {
        List<Permission> permissionList = permissionService.listAll();
        PageInfo pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("permissionList", permissionList);
        return "permission/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do";
    }

    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
        try {
            permissionService.reroad();
            return new JsonResult(true,"恭喜,权限加载成功了");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"您没有权限操作");
        }

    }
}